package com.epfresh.supplier.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Scroller;
import android.widget.Toast;

public class MyLoadMoreScrollView extends LinearLayout {
    private int index = 0;
    /**
     * 内容高度
     */
    int measuredHeight;
    /**
     * 显示高度
     */
    int height;

    /**
     * 滑动辅助类
     */
    Scroller mScroller;
    /**
     * 瞬时滑动速度测量类
     */
    VelocityTracker mVelocityTracker;


    public MyLoadMoreScrollView(Context context) {
        super(context);
        setOrientation(VERTICAL);
        mScroller = new Scroller(context);
    }

    public MyLoadMoreScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);
        mScroller = new Scroller(context);
    }

    public MyLoadMoreScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
        mScroller = new Scroller(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measuredHeight = 0;
        //得到控件原始显示高度
        height = getMeasuredHeight();
        //得到高度模式 如果是未指定大小 那么重新测量控件的大小
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.UNSPECIFIED) {
            int count = getChildCount();
            //循环所有子控件重新测量并获得高度
            for (int i = 0; i < count; i++) {
                View v = getChildAt(i);
                measureChild(v, widthMeasureSpec, heightMeasureSpec);
                //注意获取子控件的margin
                MarginLayoutParams lp = (MarginLayoutParams) v.getLayoutParams();
                //加上所有的子控件的高度 就是我们当前控件所展示的高度
                measuredHeight += v.getMeasuredHeight() + lp.bottomMargin + lp.topMargin;
            }
            //调用此方法 重新更改高度
            setMeasuredDimension(getMeasuredWidth(), measuredHeight);
        } else {
            measuredHeight = height;
        }

    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                index++;
                break;
            default:
                break;
        }
        if (event.getAction() == MotionEvent.ACTION_UP && index > 0) {
            index = 0;
            View view = getChildAt(0);
            if (view.getMeasuredHeight() <= getScrollY() + getHeight()) {
                //加载数据代码
                Toast.makeText(getContext(), "加载更多111", Toast.LENGTH_SHORT).show();
            }
        }
        return true;
    }


    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }
    }

}
